package fit.conceptmap.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;

import org.springframework.stereotype.Repository;

import fit.conceptmap.beans.AssignmentBean;
import fit.conceptmap.dao.IAssignmentDao;
import fit.conceptmap.entities.Assignment;
import fit.conceptmap.entities.Conceptmap;
import fit.conceptmap.entities.MemberAssignment;
import fit.conceptmap.entities.Relationship;
import fit.conceptmap.entities.User;
import fit.conceptmap.utils.DrawUtils;

@Repository
public class AssignmentDaoImpl implements IAssignmentDao {

	@PersistenceContext
	private EntityManager em;

	@Override
	@Transactional
	public boolean createAssignment(AssignmentBean assignment)
			throws ParseException {
		boolean result = true;
		try {
			Assignment entity = new Assignment();
			entity.setIdAssignment(assignment.getIdAssignment());
			entity.setName(assignment.getName());
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			Date startDate = sdf.parse(assignment.getStartTime());
			Date deadline = sdf.parse(assignment.getDeadline());
			entity.setStartTime(startDate);
			entity.setDeadLine(deadline);
			User user = em.find(User.class, assignment.getOwner());
			entity.setUser(user);
			Conceptmap conceptmap = em.find(Conceptmap.class, assignment.getIdConceptMaps());
			entity.setConceptmap(conceptmap);
			em.persist(entity);
		} catch (Exception ex) {
			System.out.println(ex.toString());
			result = false;
		}
		return result;
	}

	@Override
	@Transactional
	public boolean createMemberAssignment(String idMember, AssignmentBean assignment)
			throws ParseException {
		boolean result = true;
		try {
			Assignment assmnt = em.find(Assignment.class, assignment.getIdAssignment());
			MemberAssignment member = new MemberAssignment();
			member.setAssignment(assmnt);
			User user = em.find(User.class, idMember);
			member.setUser(user);
			member.setEndDate(assmnt.getDeadLine());
			member.setIdmember_Assignment(assignment.getIdAssignment() + "_" + user.getIdUser());
			em.persist(member);
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	@Override
	public List<Conceptmap> getListExercise(String username) {
		// Get member assignment
		String strQuery1 = "Select p From MemberAssignment p Where p.user.idUser = :username";
		TypedQuery<MemberAssignment> query = em.createQuery(strQuery1, MemberAssignment.class);
		query.setParameter("username", username);
		List<MemberAssignment> members =  query.getResultList();
		// Get Assignment
		List<Assignment> assignments = new ArrayList<Assignment>();
		for (MemberAssignment memberAssignment : members) {
			String strQuery = "Select p From Assignment p Where p.idAssignment = :idAssignment";
			TypedQuery<Assignment> query2 = em.createQuery(strQuery, Assignment.class);
			query2.setParameter("idAssignment", memberAssignment.getAssignment().getIdAssignment());
			Assignment assignment =  query2.getSingleResult();
			assignments.add(assignment);
		}
		// Get Conceptmap
		List<Conceptmap> conceptmaps = new ArrayList<Conceptmap>();
		for (Assignment assignment : assignments) {
			String strQuery = "Select p From Conceptmap p Where p.idConceptMaps = :idConceptMaps";
			TypedQuery<Conceptmap> query2 = em.createQuery(strQuery, Conceptmap.class);
			query2.setParameter("idConceptMaps", assignment.getConceptmap().getIdConceptMaps());
			Conceptmap conceptmap =  query2.getSingleResult();
			conceptmaps.add(conceptmap);
		}
		return conceptmaps;
	}

	@Override
	public Assignment getAssignment(String username, String conceptMapsId) {
		Assignment result = null;
		String strQuery = "Select p From Assignment p Where p.conceptmap.idConceptMaps = :idConceptMaps";
		TypedQuery<Assignment> query = em.createQuery(strQuery, Assignment.class);
		query.setParameter("idConceptMaps", conceptMapsId);
		List<Assignment> lst = query.getResultList();	
		for (Assignment assignment : lst) {
			String str = "Select p From MemberAssignment p Where p.assignment.idAssignment = :idAssignment AND p.user.idUser = :username";
			TypedQuery<MemberAssignment> query2 = em.createQuery(str, MemberAssignment.class);
			query2.setParameter("idAssignment", assignment.getIdAssignment());
			query2.setParameter("username", username);
			if(!query2.getResultList().isEmpty())
				return result = assignment;
		}
		return result;
	}

}
